#include "stdafx.h"
#include "TimeCounter.hpp"

HpsFramework::Utilities::TimeCounter::TimeCounter() : _started(false)
{
	this->_startTime.QuadPart = 0;
	this->_finishTime.QuadPart = 0;
	QueryPerformanceFrequency(&this->_frequency);
}

void HpsFramework::Utilities::TimeCounter::start()
{
	if(this->_started) 
		return;
	this->restart();
}

void HpsFramework::Utilities::TimeCounter::restart()
{
	QueryPerformanceCounter(&this->_startTime);
	this->_started = true;
}

void HpsFramework::Utilities::TimeCounter::finish()
{
	if(!this->_started) 
		return;
	QueryPerformanceCounter(&this->_finishTime);
	this->_started = false;
}

double HpsFramework::Utilities::TimeCounter::getElapsed()
{
	LARGE_INTEGER elapsed;
	if(this->_started) 
	{
		LARGE_INTEGER last;
		QueryPerformanceCounter(&last);
		elapsed.QuadPart = last.QuadPart - this->_startTime.QuadPart;
	} 
	else 
	{
		elapsed.QuadPart = this->_finishTime.QuadPart - this->_startTime.QuadPart;
	}
	if(this->_frequency.QuadPart != 0)
	{
		return ((double)elapsed.QuadPart / (double)this->_frequency.QuadPart);
	}
	return 0;
}
